<template>
	<el-container>
		<div class="left-panel">
			<div class="right-panel-search" style="width: 1150px;margin-top: 5px;margin-bottom: 5px">
				<span class="demonstration" style="margin-left: 50px">启停状态</span>
				<el-select v-model="search.tag" placeholder="全部" style="width: 150px;margin-left: 20px">
					<el-option label="启用" value="1"/>
					<el-option label="停用" value="2"/>
				</el-select>

				<el-button type="primary" style="margin-left: 20px" @click="upsearch" v-if="!index">查询</el-button>
				<el-button  type=""  @click="upsearch" v-if="!index">清空</el-button>
				<div style="padding-left: 40px;color: #666666;font-size: 14px;cursor: pointer;" type=""  @click="index=!index" v-if="!index">更多<el-icon><el-icon-arrow-up/></el-icon></div>
				<div  style="color: #666666;font-size: 14px;cursor: pointer;" type=""  @click="index=!index" v-if="index">收起<el-icon><el-icon-arrow-up/></el-icon></div>
			</div>
			<div v-if="index" style="margin-top: 5px;margin-bottom: 5px;display: flex;align-items: center;">
				<div style="margin-left: 20px;">按名称</div>
				<el-input  style="width: 509px;margin-left: 32px" v-model="search.name" placeholder="请输入题目标题" clearable ></el-input>
				<el-button style="margin-left: 40px" type="primary"  @click="upsearch" v-if="index">查询</el-button>
				<el-button type=""  @click="upsearch" v-if="index">清空</el-button>
			</div>

		</div>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" icon="el-icon-plus" @click="add"></el-button>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="apiObj" row-key="id" @selection-change="selectionChange" hidePagination>
				<el-table-column label="组织名称" prop="label"></el-table-column>
				<el-table-column label="是否启用" prop="enable">
					<template #default="scope">
						<el-switch v-model="scope.row.enable" @change="changeSwitch($event, scope.row)" :loading="scope.row.$switch_status" :active-value="0" :inactive-value="1"></el-switch>
					</template>
				</el-table-column>
				<el-table-column label="组织编码" prop="num"></el-table-column>
<!--				<el-table-column label="备注" prop="remark" min-width="300"></el-table-column>-->
				<el-table-column label="操作" fixed="right" align="right">
					<template #default="scope">
						<el-button-group>
							<el-button text type="primary" size="small" @click="table_show(scope.row, scope.$index)">查看</el-button>
							<el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)">编辑</el-button>
							<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
								<template #reference>
									<el-button text type="primary" size="small">删除</el-button>
								</template>
							</el-popconfirm>
						</el-button-group>
					</template>
				</el-table-column>

			</scTable>
		</el-main>
	</el-container>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false">

	</save-dialog>
	<edit-dialog v-if="dialog.edit" ref="editDialog" @success="handleSaveSuccess" @closed="dialog.edit=false">

	</edit-dialog>
</template>

<script>
	import saveDialog from './add'
	import editDialog from '../../basic/org/edit'
	export default {
		name: 'dept',
		components: {
			saveDialog,
			editDialog
		},
		data() {
			return {
				index:false,
				dialog: {
					save: false,
					edit:false
				},
				apiObj: this.$API.user.dept.list,
				selection: [],
				search: {
					keyword: null
				}
			}
		},
		methods: {
			//添加
			add(){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open()
				})
			},
			//编辑
			table_edit(row){
				this.dialog.edit = true
				this.$nextTick(() => {
					this.$refs.editDialog.open('edit').setData(row)
				})
			},
			//查看
			table_show(row){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('show').setData(row)
				})
			},
			//删除
			async table_del(row){
				var reqData = {id: row.id}
				var res = await this.$API.user.dept.del.post({data:reqData});
				if(res.code == 0){
					this.$refs.table.refresh()
					this.$message.success("删除成功")
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			//表格内开关
			async changeSwitch(val, row){

				row.enable = row.enable == 1?0:1
				row.$switch_status = true;
				let data = {
					enable: val,
					id: row.id
				}
				var res = await this.$API.user.dept.enable.post({data:data});
                if (res && res.code == 0) {
					setTimeout(()=>{
						delete row.$switch_status;
						row.enable = val;
						// console.log(typeof val, row)
						this.$message.success("操作成功")
					}, 500)
				}

			},
			//批量删除
			async batch_del(){
				this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？如果删除项中含有子集将会被一并删除`, '提示', {
					type: 'warning'
				}).then(() => {
					const loading = this.$loading();
					this.$refs.table.refresh()
					loading.close();
					this.$message.success("操作成功")
				}).catch(() => {

				})
			},
			//表格选择后回调事件
			selectionChange(selection){
				this.selection = selection;
			},
			//搜索
			upsearch(){

			},
			//根据ID获取树结构
			filterTree(id){
				var target = null;
				function filter(tree){
					tree.forEach(item => {
						if(item.id == id){
							target = item
						}
						if(item.children){
							filter(item.children)
						}
					})
				}
				filter(this.$refs.table.tableData)
				return target
			},
			//本地更新数据
			handleSaveSuccess(data, mode){
				if(mode=='add'){
					this.$refs.table.refresh()
				}else if(mode=='edit'){
					this.$refs.table.refresh()
				}
			}
		}
	}
</script>

<style>
</style>
